#coding: utf-8 
#       room.py
#       
#       Copyright 2010 Vasco Rio <vasco@vasco-laptop>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

from simData import *
from label import *
from geneticRoom import *

from cleanAlgorithm import *

class Room():
	def __init__(self,name):
		self.size=None
		self.name='r'+str(name)
		
		self.lastClean = None
		self.ntbc = True
		
		self.connected=False
		
		self.parent=None
		self.points=[]
		self.area=None
		
		self.sum_x=0.
		self.sum_y=0.
		self.count_px=0.
		
		self.label=None
		self.label_text=""
		self.update(False)
		
		self.areas={}
		
		
	
	def needToBeCleaned(self,time):
		if self.lastClean==None:
			return True
		else:
			self.ntbc=time-self.lastClean>CLEAN_ROOM_EVERY_SECONDS
			return self.ntbc
	def set_cleaned(self,time):
		self.lastClean=time
		self.ntbc=False
	
	def defineArea(self):
		points = self.points
		self.area=Area(CLEANGRID,0,0)
		
		
		for x,y in points:
			self.area.setitem(x,y,True)
		
		#~ self.area=self.area.removeBorder()
		self.area.clear()
		
		self.areas=self.area.cellDecomp()
		for area in self.areas.values():
			area.genAllPositionsOrdS()
			area.genAllPositionsOrdCirc()
		
	def defineGeneticAlg(self,population,generations):
		try:
			winner=self.winner
			#~ print winner
			self.searchAC=RoomSearchAreaCovering(self,population,generations,winner)
		except:
			self.searchAC=RoomSearchAreaCovering(self,population,generations)
		
	def winnerPath(self):
		try:
			return getPositionsFrom(self, self.winner)
		except:
			print self, "Ainda não se sabe o melhor caminho."
			return []
	
	def runGenOptim(self,robot=None,population=2,generations=2):
		if robot is None:
			from robot import ROMBA_MODEL,Robot
			from pygame import Rect
			self.robot=GeneticRobot(self.parent.cleanSim,10.0,5.0,0.0,500.0,0,ROMBA_MODEL)
			self.robot.original = Robot.hidden_img
			self.robot.image = Robot.hidden_img
			self.robot.rect = self.robot.image.get_rect()
			robot = self.robot
		if robot.isBeingControledBy is None:
			robot.isBeingControledBy = self
			
			self.defineGeneticAlg(population,generations)
			self.searchAC.setRobot(robot)
			self.searchAC.start()
		else:
			print "Unable to control Robot. He is already being controled by", robot.isBeingControledBy
		
	
	
	
		
	def __repr__(self):
		txt = str(self.name)#"Room "+str(self.name) +": "+ str(self.pos)
		if not self.connected:
			txt+=" not connected"
		return txt
	
	@property
	def winner(self):
		try:
			return self.searchAC.winner
		except:
			return self.win
	
	@property
	def positions(self):
		return self.area.getPositions()
		

	def loadSprites(self,parent):
		self.parent=parent
		centerx,centery=self.parent.getPixelInScreen(self.pos)
		self.label=Label(text=str(self.name),centerx=centerx,centery=centery,color=[255,0,0])

	def update(self,visible):
		if self.ntbc:
			color=[255,0,0]
		else:
			color=[0,125,0]
		try:
			centerx,centery=self.parent.getPixelInScreen(self.pos)
			if visible and self.connected:
				self.label.update(text=str(self.name),centerx=centerx,centery=centery,color=color)
			else:
				self.label.update(text='',centerx=centerx,centery=centery,color=color)
		except:
			pass

